#include "asnprefix.hpp"

AsnPrefix::AsnPrefix( uint32_t _asn, Prefix _prefix ) {
    asn = _asn;
    prefix = _prefix;
}

uint32_t AsnPrefix::getAsn() const { return asn; }
Prefix AsnPrefix::getPrefix() const { return prefix; }

string AsnPrefix::str( string delimiter) const 
{
    ostringstream oss;
    oss << asn << delimiter  << prefix.str();
    return oss.str();
}

bool AsnPrefix::operator==( const AsnPrefix & rhs ) const {
    return ( asn == rhs.asn ) && ( prefix == rhs.prefix);
}

bool AsnPrefix::equal( const FlowMapKey  & rhs ) const 
{
    return *this == dynamic_cast<const AsnPrefix &>(rhs);
}

size_t AsnPrefix::hash_self() const
{
    struct {
        uint32_t asn;
        uint32_t prefix_ip;
        uint8_t  prefix_netmask;
        } buf;
    memset(&buf,0,sizeof(buf)); /*XXX super importaint! struct padding bytes must be zero*/
    buf.asn = this->getAsn();
    buf.prefix_ip = this->getPrefix().getIP();
    buf.prefix_netmask = this->getPrefix().getNetmask();
    return (size_t) sfhash ( (char*)&buf, sizeof(buf) );
}

AsnAsn::AsnAsn( uint32_t _asn_src, uint32_t _asn_dst)
{
    asn_src = _asn_src;
    asn_dst = _asn_dst;
}

uint32_t AsnAsn::getAsnSrc() const
{
    return asn_src;
}

uint32_t AsnAsn::getAsnDst() const
{
    return asn_dst;
}

string AsnAsn::str( string delimiter = " ") const
{
    ostringstream oss;
    oss << asn_src << delimiter  << asn_dst;
    return oss.str();
}

bool AsnAsn::operator==( const AsnAsn & rhs ) const
{
    return ( asn_src == rhs.asn_src ) && ( asn_dst == rhs.asn_dst );
}

bool AsnAsn::equal( const FlowMapKey & rhs ) const
{
    return *this == dynamic_cast<const AsnAsn &>(rhs);
}

size_t AsnAsn::hash_self() const
{
    struct {
        uint32_t asn_src;
        uint32_t asn_dst;
        } buf;
    memset(&buf,0,sizeof(buf)); /*XXX super importaint! struct padding bytes must be zero*/
    buf.asn_src = this->getAsnSrc();
    buf.asn_dst = this->getAsnDst();
    return (size_t) sfhash ( (char*)&buf, sizeof(buf) );
}

